require 'rho/rhocontroller'
require 'helpers/browser_helper'
require 'json'

class StudentController < Rho::RhoController
  include BrowserHelper
  $image = nil
  def take_picture
    Camera::take_picture(url_for(:action => :camera_callback))
     redirect :action => :conversation
  end

  def select_picture
    Camera::choose_picture(url_for(:action => :camera_callback))
    redirect :action => :conversation
  end

  def camera_callback
    if @params['status'] == 'ok'
    $image = Image.new({'image_uri' => @params["image_uri"], 'is_blob' => true})
        
      WebView.navigate(url_for(:action => :conversation, :controller => :Question)) 
    else
      Alert.show_popup @params['status']
    end
  end
  
  def conversation
    @image = $image unless $image.nil?
    render :action => :conversation
  end
  
  def conversation_list
    q_id = @params["q_id"].to_i
    question = Question.find(:first, :conditions => {:id => q_id})
    @conversations = {}
    @conversations["q_content"] = question.content unless question.nil?
    @conversations["s_id"] = $current_user.id
    #get list of teacher answered this question
    qa_details = QADetail.find(:all, :conditions => ["user_id <> ? AND q_id = ?", $current_user.id, q_id], 
      :order => 'created_at')  
    @conversations["data"] = {}  
    #assign it to hash to make it unique
    qa_details.each do |qa_detail|
      
      if @conversations["data"].has_key? qa_detail.user_id
        next
      else
        @conversations["data"][qa_detail.user_id] = qa_detail
      end
    end
    
    
  end
  
  def questions
    response = Rho::AsyncHttp.get(
      :url => WEBSERVER_URL + "get_student_questions",
      :body => "user_id=#{$user['id']}"
    )
    
    @questions = response["body"]
    render :action => :questions
  end
  
  
  
  def handwriting
    if $image
      @image = $image
    end
    render :action => :handwriting
  end
  
  def ask_question
    @grades = []
    url = BaseService::SERVER_URL
    @grades = Rho::AsyncHttp.get(
      :url => "#{url}/grades.json" 
    )['body']


    @subjects = []
    url = BaseService::SERVER_URL
    @subjects = Rho::AsyncHttp.get(
      :url => "#{url}/subjects.json" 
    )['body'] || []

    render :action => :ask_question
  end
  
  def do_ask_question
    #Save new question here
    param_string = {
        :question => {
          :subject_id => @params["question"]["subject_id"],
          :grade_id => @params["question"]["grade_id"],
          :student_id => $user["id"],
          :title => @params["question"]["content"]         
        }
      }.to_json

    @params["question"]["user_id"] = $user["id"]
    Rho::AsyncHttp.post(
      :url => WEBSERVER_URL + "create_question",
      :headers => {"content-type" => "application/json"},
      :body => param_string
    )
      
    redirect :action => :questions
  end

  def conversation_list
    student_id = $user['id']
    @question_id = @params["question_id"]
    response = Rho::AsyncHttp.get(
      :url => WEBSERVER_URL + "get_teacher_answered",
      :body => "student_id=#{student_id}&question_id=#{@question_id}"
      )

    @users = response["body"]

    render :action => :conversation_list

  end
  
  def profile
    @profile = $user
    
    render :action => :profile
  end
  
  def add_money
    @profile = $user
        
    render :action => :add_money
  end
  
  def post_add_money
    @ma_the = @params['ma_the']
    @user_id = $user['id']
      
    url = "http://10.10.10.154:3000/add_coin"
    param_string = {:user_id => @user_id, :code => @ma_the}.to_json
    
        response = Rho::AsyncHttp.post(
        :url => url,
        :headers => {"content-type" => "application/json"},
        :body => param_string
        )
    
        total_point = response["body"]["point"].to_i
          
        $user['point'] = total_point.to_s
          
        redirect url_for(:action => :profile, :controller => :Student)
  end

  def index
    @profile = $user

    @number_of_questions = 0

    @user_id = $user['id']

    url = "http://10.10.10.154:3000/unanswered_question_count?user_id=#{@user_id}"

    response = Rho::AsyncHttp.get(
    :url => url
    )

    @number_of_questions = response["body"]["count"].to_i

    render :action => :index
  end
  
end
